Skip to content

perf(sanitizer): avoid Token() allocations in stripIter#4443

Merged
fguillot merged 1 commit into
miniflux:mainfrom
jvoisin:stroptogs
Jul 8, 2026
Merged

perf(sanitizer): avoid Token() allocations in stripIter#4443
fguillot merged 1 commit into
miniflux:mainfrom
jvoisin:stroptogs

Conversation

@jvoisin

@jvoisin jvoisin commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Tokenizer.Token() allocates a Token struct and parses the full attribute slice for every start tag, even though stripIter only consumes text tokens. Switch to checking the TokenType from Next() and calling Text() only for text tokens.

Shared by StripTags and TruncateHTML, both called per entry:

StripTags 247KB -> 71KB/op (-71%), 12623 -> 4423 allocs (-65%)
TruncateHTML 19KB -> 7.9KB/op (-59%), 819 -> 297 allocs (-64%)

Both roughly 2x faster on tag-heavy input.

Have you followed these guidelines?

Tokenizer.Token() allocates a Token struct and parses the full
attribute slice for every start tag, even though stripIter only
consumes text tokens. Switch to checking the TokenType from Next()
and calling Text() only for text tokens.

Shared by StripTags and TruncateHTML, both called per entry:

  StripTags     247KB -> 71KB/op (-71%), 12623 -> 4423 allocs (-65%)
  TruncateHTML  19KB  -> 7.9KB/op (-59%),   819 ->  297 allocs (-64%)

Both roughly 2x faster on tag-heavy input.
@fguillot fguillot merged commit e43381d into miniflux:main Jul 8, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants